package com.cn.jinl.service;

import com.cn.jinl.api.IOnDutyPersonService;
import com.cn.jinl.api.IOnDutyWeekService;
import com.cn.jinl.dao.OnDutyWeekMapper;
import com.cn.jinl.domain.OnDutyWeek;
import com.cn.jinl.domain.OnDutyWeekExample;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OnDutyWeekServiceImpl implements IOnDutyWeekService {

    @Autowired
    private OnDutyWeekMapper onDutyWeekMapper;

    @Autowired
    private IOnDutyPersonService onDutyPersonService;

    @Override
    public List<OnDutyWeek> getOnDutyWeekList() throws Exception {
        return onDutyWeekMapper.selectOnDutyWeeks();
    }

    @Override
    public String addOnDutyWeek(OnDutyWeek onDutyWeek) throws Exception {
        String dutyDay = onDutyWeek.getDutyDay();
        onDutyWeek.setId(CommonUtil.generateRandomNum("ondutyweek-"));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if(StringUtils.isEmpty(dutyDay)){
            dutyDay = simpleDateFormat.format(new Date());
        }
        int existCount = onDutyWeekMapper.selectOnDutyWeekByWeek(dutyDay);
        if(existCount > 0){
            return "记录已存在，请刷新重试1";
        }
        if(onDutyWeekMapper.selectAllOnDutyWeek() >= 4){
            return "最多添加4个，请删除后再添加！";
        }
        Date currentDate = simpleDateFormat.parse(dutyDay);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        onDutyWeek.setWeek(CommonUtil.convertToWeekday(dayOfWeek));
        onDutyWeek.setDutyDay(dutyDay);

        int count = onDutyWeekMapper.insertSelective(onDutyWeek);
        if(count < 1){
            return "插入日期失败！";
        }
        String resp = onDutyPersonService.addOnDutyPerson(dutyDay);
        if(StringUtils.isNotEmpty(resp)){
            throw new Exception(resp);
        }
        return "";
    }

    @Override
    public String deleteOnDutyWeek(String id) throws Exception {
        OnDutyWeek exist = onDutyWeekMapper.selectOnDutyWeekById(id);
        if(exist == null){
            return "该日期不存在！";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date());
        if(StringUtils.equals(today,exist.getDutyDay())){
            return "今天记录不能删除！";
        }
        OnDutyWeek update = new OnDutyWeek();
        update.setStatus(Constants.DEL_STATUS);
        OnDutyWeekExample example = new OnDutyWeekExample();
        example.createCriteria().andIdEqualTo(id);
        int count = onDutyWeekMapper.updateByExampleSelective(update,example);
        if(count > -1){
            String resp = onDutyPersonService.deleteOnDutyPersonByDay(exist.getDutyDay());
            if(StringUtils.isNotEmpty(resp)){
                throw new Exception(resp);
            }
        }
        return CommonUtil.outStr(count);
    }
}
